package com.hxx.controller;

import com.hxx.domain.Person;
import com.hxx.util.HxxUtils;

import java.util.Scanner;

public class PersonController {
    //数据库：数组
    private Person[] persons = new Person[5];
    //按照id查找
    public int findPersonById(Scanner scanner) {
        System.out.println("请输入要查找的id：");
        int id = scanner.nextInt();
        for (int index = 0; index < persons.length; index++) {
            if (id==0){
                if (persons[index] == null) {
                    return index;
                }
            }else{
                if (persons[index] != null && persons[index].getId() == id) {
                    return index;
                }
            }
        }
        return -1;
    }
    //添加人
    public void addPerson(Scanner scanner){
        int index = findPersonById(scanner);
        if (index == -1){
            System.out.println("添加失败，已满员！");
        }else {
            Person person = new Person();
            person.setId(HxxUtils.generateId());
            System.out.print("请输入姓名：");
            person.setUsername(scanner.next());
            System.out.print("请输入年龄：");
            person.setAge(scanner.nextInt());
            System.out.print("请输入性别：");
            person.setSex(scanner.next());
            persons[index] = person;
            System.out.println("添加成功！");
        }
    }

    //删除人
    public void deletePerson(Scanner scanner){
        int index = findPersonById(scanner);
        if (index == -1){
            System.out.println("删除失败，不存在该人！");
        }else {
            persons[index] = null;
            System.out.println("删除成功！");
        }
    }

    //修改人
    public void updatePerson(Scanner scanner){
        int index = findPersonById(scanner);
        if (index == -1) {
            System.out.println("修改失败，不存在该人！");
        }else {
            Person person = persons[index];

            System.out.print("请输入姓名：");
            person.setUsername(scanner.next());
            System.out.print("请输入年龄：");
            person.setAge(scanner.nextInt());
            System.out.print("请输入性别：");
            person.setSex(scanner.next());
            //persons[index] = person;
            System.out.println("修改成功！");
        }
    }

    //查询人
    public void queryPerson(){
        for (int index = 0; index < persons.length; index++) {
            Person person = persons[index];
            if (person != null){
                System.out.println(person);
            }
        }
    }



}




























